<template>
  <div class="container">
    <el-card>
      <div class="heard-breadcrumb" v-if="dirRoute.id">
        <el-row>
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{ dirRoute.name }}</el-breadcrumb-item>
            <el-breadcrumb-item>目录管理</el-breadcrumb-item>
          </el-breadcrumb>
        </el-row>
      </div>
      <div class="body-main">
        <!-- 头部搜索 -->
        <el-row type="flex" justify="center">
          <el-col :span="20">
            <el-form :inline="true" ref="directoryRef" :model="searchParameter" label-width="80px">
              <el-form-item label="目录名称" prop="directoryName">
                <el-input v-model="searchParameter.directoryName" size="small"></el-input>
              </el-form-item>
              <el-form-item label="状态" prop="state">
                <el-select v-model="searchParameter.state" placeholder="请选择" size="small">
                  <el-option label="启用" :value="1"></el-option>
                  <el-option label="禁用" :value="0"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button size="small" @click="cleanBtn">清除</el-button>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" size="small" @click="searchBtn">搜索</el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <el-button v-if="dirRoute.id" type="text" icon="el-icon-back" @click="$router.go(-1)">返回学科</el-button>
              <el-button type="success" icon="el-icon-edit" size="small" @click="openDialog">新增目录</el-button>
            </el-row>
          </el-col>
        </el-row>
        <!-- 显示条数 -->
        <el-alert :title="`数据一共${total}条`" type="info" show-icon :closable="false" style="margin-bottom: 20px" />
        <!-- 表格 -->
        <template>
          <el-table :data="tableData" style="width: 100%" :header-cell-style="tabStyle" :row-style="trStyle">
            <el-table-column label="序号" width="80" type="index" />
            <el-table-column label="所属学科" prop="subjectName" />
            <el-table-column label="目录名称" prop="directoryName" />
            <el-table-column label="创建者" prop="username" />
            <el-table-column label="创建日期">
              <template #default="{ row }">
                {{ formatDate(row.addDate) }}
              </template>
            </el-table-column>
            <el-table-column label="面试题数量" prop="totals" />
            <el-table-column label="状态" prop="state">
              <template #default="{ row }">
                {{ isState[row.state] }}
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template #default="{ row }">
                <el-button type="text" size="small" @click="stateBtn(row)">{{ !!row.state ? '禁用' : '启用' }}</el-button>
                <el-button type="text" size="small" :disabled="!!row.state" @click="openEditDialog(row.id)">修改</el-button>
                <el-button type="text" size="small" :disabled="!!row.state" @click="delDirectorys(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <!-- 分页 -->
        <el-row v-if="total > 10" type="flex" justify="end" style="margin-top: 20px">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="parameter.page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="parameter.pagesize"
            layout="prev, pager, next,sizes, jumper"
            :total="total"
          >
          </el-pagination>
        </el-row>
      </div>
    </el-card>
    <!-- 学科弹窗 -->
    <directorys-add :dirRoute="dirRoute" :dialogVisible="dialogVisible" :curId="curId" @dialogClose="dialogClose" @getDirectorysList="getDirectorysList" />
  </div>
</template>

<script>
import { list as getDirectorysList, remove, changeState } from '@/api/hmmm/directorys'
import DirectorysAdd from '../components/directorys-add.vue'
import dayjs from 'dayjs'
export default {
  components: {
    DirectorysAdd
  },
  data() {
    return {
      // 表格数据
      tableData: [],
      // 表格头样式
      tabStyle: {
        background: '#fafafa',
        borderBottom: '2px solid #e8e8e8'
      },
      // 表格行样式
      trStyle: {
        height: '67px'
      },
      // 分页参数
      parameter: {
        page: 1,
        pagesize: 10
      },
      // 搜索参数
      searchParameter: {
        directoryName: undefined,
        state: undefined,
        subjectID: undefined
      },
      // 总条数
      total: 0,
      // 是否显示前台的映射
      isState: {
        1: '已启用',
        0: '已禁用'
      },
      // 控制弹窗显示
      dialogVisible: false,
      // 点击的id
      curId: '',
      // 跳转传参
      dirRoute: {}
    }
  },
  created() {
    this.dirRoute = this.$route.query
    this.searchParameter.subjectID = this.$route.query.id
    this.getDirectorysList()
  },
  watch: {
    $route(to, from) {
      if (to.fullPath == to.path) {
        this.searchParameter.subjectID = undefined
        this.getDirectorysList()
        this.dirRoute = {}
      }
    }
  },
  methods: {
    // 获取目录列表
    async getDirectorysList() {
      const res = await getDirectorysList({ ...this.parameter, ...this.searchParameter })
      this.tableData = res.data.items
      this.total = res.data.counts
    },
    handleSizeChange(size) {
      this.parameter.pagesize = size
      this.getDirectorysList(this.parameter)
    },
    handleCurrentChange(page) {
      this.parameter.page = page
      this.getDirectorysList(this.parameter)
    },
    // 格式化时间
    formatDate(val) {
      return dayjs(val).format('YYYY-MM-DD HH:mm:ss')
    },
    // 清除
    cleanBtn() {
      this.$refs.directoryRef.resetFields()
    },
    // 搜索
    searchBtn() {
      this.parameter.page = 1
      this.getDirectorysList()
    },
    // 禁用按钮
    async stateBtn(row) {
      !!row.state ? (row.state = 0) : (row.state = 1)
      await changeState(row)
      this.$message.success('操作成功')
    },
    // 打开弹窗
    openDialog() {
      this.dialogVisible = true
    },
    // 打开编辑弹窗
    openEditDialog(id) {
      this.dialogVisible = true
      this.curId = id
    },
    // 关闭弹窗
    dialogClose() {
      this.dialogVisible = false
      this.curId = ''
    },
    // 删除学科
    delDirectorys(data) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await remove(data)
          console.log(res)
          this.$message.success('删除成功')
          this.getDirectorysList()
        })
        .catch(() => {})
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 0 10px;
  margin: 10px 0;
  ::v-deep .el-pagination__jump {
    margin-left: 0px;
  }
  ::v-deep .el-card__body {
    padding: 0 !important;
  }
  .body-main {
    padding: 20px;
  }
  .heard-breadcrumb {
    padding: 18px 20px;
    border-bottom: 0.5px solid #ebeef5;
  }
}
</style>

